import { getInterfacceDetail } from '@/api/test';
import { Agreement, dataType, isDataRequired, Position, RequestMethod } from '@/enums/statusEnum';
import { addingApiParamterConfig, apiDetailType } from '@/pages/CodeTables/specification';
import { cloneDeep } from 'lodash-es';

// 控制详情
const dislog = reactive({
  details: false, //详情弹窗
  Classification: false, //分类弹窗
  test: false, //接口测试弹窗
});
//接收参数
const apiDetail = ref<apiDetailType>({
  apiAgreement: '',
  apiDescribe: '',
  apiDirectoryCode: '',
  apiIpPort: '10.8.21.156:9999',
  apiName: '',
  apiPath: '/',
  apiRequestMethod: '',
  apiRequestTimeout: 30,
});
// 添加验证字段
const addVerify = (data: addingApiParamterConfig) => {
  ['apcNameVerify', 'apcDataTypeVerify', 'apcDataNullVerify', 'apcPositionVerify'].forEach(item => {
    data[item] = {
      message: '',
      status: 'success',
    };
  });
};
// 验证填写数据是否合法
const isValid = (record: addingApiParamterConfig, dataIndex: string[]) => {
  const flag = ref<boolean>(true);
  dataIndex.forEach(item => {
    if (record[item] == '' || record[item] == undefined) {
      record[`${item}Verify`] = {
        message: '不能为空',
        status: 'error',
      };
      flag.value = false;
    } else {
      record[`${item}Verify`] = {
        message: '',
        status: 'success',
      };
    }
  });
  return flag.value;
};
// 接口编号
const apiCode = ref<string>('');
// 第二页显示列表
const bodyList = ref<addingApiParamterConfig[]>([]);
const paramList = ref<addingApiParamterConfig[]>([]);
const resultList = ref<addingApiParamterConfig[]>([]);
//处理返回类型显示, 删除children为[]的children,添加父节点parent
const generateList = (data: addingApiParamterConfig[], parent?: addingApiParamterConfig) => {
  // console.log(`data: ${JSON.stringify(data)}`);

  for (let i = 0; i < data.length; i++) {
    const { children = [], apcDataNull, apcDataType, apcPosition } = data[i];
    addVerify(data[i]);
    data[i].apcDataNull = isDataRequired[apcDataNull as number];
    data[i].apcDataType = dataType[apcDataType as number];
    data[i].apcPosition = Position[apcPosition as number];
    data[i].parent = parent;
    if (children.length) {
      generateList(children as [], data[i]);
    } else {
      delete data[i].children;
    }
  }
};
//处理发送类型, 将后端需要的数据转换为需要的枚举值,删除父节点parent
const dealGenerateList = (data: addingApiParamterConfig[]) => {
  for (let i = 0; i < data.length; i++) {
    const { children = [], apcDataNull, apcDataType, apcPosition } = data[i];
    data[i].apcDataNull = isDataRequired[apcDataNull as number];
    data[i].apcDataType = dataType[apcDataType as number];
    data[i].apcPosition = Position[apcPosition as number];
    delete data[i].parent;
    if (children.length) {
      dealGenerateList(children as []);
    }
  }
};

// 统一处理发送后端数据
const dispose = () => {
  const validateInfo = cloneDeep(apiDetail.value);
  console.log(bodyList.value);

  validateInfo.createOrUpdateApiParamterConfigurationList = cloneDeep([...paramList.value, ...bodyList.value, ...resultList.value]);

  console.log([...paramList.value, ...bodyList.value, ...resultList.value]);
  console.log(validateInfo.createOrUpdateApiParamterConfigurationList);

  validateInfo.apiRequestMethod = RequestMethod[validateInfo.apiRequestMethod as number];
  validateInfo.apiAgreement = Agreement[validateInfo.apiAgreement as number];
  dealGenerateList(validateInfo.createOrUpdateApiParamterConfigurationList);
  // console.log(validateInfo.createOrUpdateApiParamterConfigurationList);
  return validateInfo;
};

// 请求详情
const getDetail = async () => {
  const res = await getInterfacceDetail(apiCode.value);
  res.apiRequestMethod = RequestMethod[+res.apiRequestMethod];
  res.apiAgreement = Agreement[+res.apiAgreement];
  bodyList.value = cloneDeep(res.bodyList);
  paramList.value = cloneDeep(res.paramList);
  resultList.value = cloneDeep(res.resultList);
  // 转换格式
  generateList(bodyList.value);
  generateList(paramList.value);
  generateList(resultList.value);
  console.log(bodyList.value);
  console.log(resultList.value);
  console.log(paramList.value);
  apiDetail.value = res as apiDetailType;
};

export function getInterfaceDetailsHooks() {
  return {
    apiCode,
    dislog,
    apiDetail,
    bodyList,
    paramList,
    resultList,
    getDetail,
    generateList,
    dispose,
    isValid,
  };
}
